var PATH_PREFIX =ctx + 'sys/oper/post/';

var WASP_POST = {
    postAddView: function() {
        /** 新建 */
    	 var actionUrl = PATH_PREFIX+"postAddView.do";
		 openDialog(actionUrl);
    },
    postUpdateView: function(postId) {
	    /** 更新 */
		 var actionUrl = PATH_PREFIX+"postUpdateView.do?postId="+postId;
		 openDialog(actionUrl);
	},
	formatViewHtml: function(text, options, rData) {
        var postId = rData["postId"];
        return '<a href="javascript(0);"  class="showunderline" data-toggle="modal" data-target="#modal-info"  onclick="WASP_POST.postDetailView(\''+postId+'\');" >' + text + '</a>';
    },
    postDetailView: function(postId){
		/**详情**/
		 var actionUrl = PATH_PREFIX+"detailView.do?postId="+postId;
		 openDialog(actionUrl);
	},
	deletePost: function(postId){
		/**删除**/
		ctools.confirm({title : "删除岗位", text: "数据删除后，不可修复"},function(isConfirm){
			var actionUrl = PATH_PREFIX+"deletePost.do?postId=" + postId;
			if(isConfirm){
				$.ajax({
					dataType: "json",
					type: 'POST',
					url: actionUrl,
					async: false,
					success: function(data){
						console.log(data);
						console.log(data.success);
						if(data.success == true){
							swal(data.message, "", "success");
							queryByCondtion(false);
						}
						
					},
					error:function(xhr){
						swal("删除出错!", "", "error");
					}
				});
			}
			
		});
	}
	
};


var $postList=$("#postList");
$(function(){
	$postList.jqGrid({   
				url:PATH_PREFIX+'postListPage.do',    
				caption:'岗位维护列表<button type="button" class="btn btn-default btn-sm btn-small btn-fullscreen" title="" tabindex="-1"><i class="fa fa-arrows-alt icon-fullscreen"></i></button>',  
				datatype: "json",
				colNames:["岗位ID","岗位名称","岗位层级","数据权限类型","是否根据岗位人员赋权","是否可在岗位页面配置","内部/外部","创建人","创建时间", '备注', '操作'],        
				colModel:[  {name:'postId',index:'POST_ID', hidden:true,key:true},
							{name:'postTitle',index:'POST_TITLE',width: 25, resizable:true,align:'left',formatter: WASP_POST.formatViewHtml,sortable:false},  
							{name:'postRankName',index:'ROLE_RANK',width: 15, resizable:true,align:'left',sortable:false},  
							{name:'authorityName',index:'AUTHORITY',width: 15, resizable:true,align:'left',sortable:false},  
							{name:'isAuthName',index:'IS_AUTH',width: 10, resizable:true,align:'left',sortable:false},  
							{name:'isConfName',index:'IS_CONF',width: 10, resizable:true,align:'left',sortable:false},  
							{name:'propertyName',index:'PROPERTY',width: 15, resizable:true,align:'left',sortable:false},  		
							/*{name:'statusName',index:'STATUS_NAME',width: 20, resizable:true,align:'left',sortable:false},*/  
							{name:'createName',index:'CREATE_NAME', width: 20, resizable:true,align:'left', sortable:false},
							{name:'createTime',index:'CREATE_TMIE', width: 30, resizable:true,align:'left', sortable:false},
							{name:'remark',index:'REMARK', width: 20, resizable:true,align:'left',sortable:false},
							{name:'option',index:'option', width: 20, resizable:true,align:'left',sortable:false}
						  ],
				rowNum: 20,
		        rowList: [20, 30, 50],
		        rownumbers: true,
		        rownumWidth: 50,
		        prmNames: { search: "search", page: "pageNo", rows: "limit" },
		        height: 'auto',
		        width: false,
		        autowidth:true,
		        editurl: '',
		        viewrecords: true,
		        cellEdit: false,
		        shrinkToFit: true,
		        grouping: false,
		        jsonReader: {
		            root: "items", //结果集
		            records: "total", //总记录数 
		            total: "pageCount", //总页数
		            page: "pageNo", //当前页 
		            repeatitems: false // (4) 
		        },  
				multiselect:false,
				pager: "#postPage", 							
				viewrecords: true,
				hidegrid: false, 
				gridComplete: function(){
					var ids = $postList.jqGrid('getDataIDs');
					for(var i=0;i < ids.length;i++){
						var role_ID = ids[i];
					 
						var rowData = $postList.jqGrid('getRowData', role_ID); 
						var ve = '<a href="#" class="btn btn-link btn-jqgrid" title="详情" onclick="WASP_POST.postDetailView(\''+rowData.postId+'\');"  >详情</a>';
						var se = '<a id="lineEdit'+role_ID+'" href="#" class="btn btn-link btn-jqgrid" title="修改" onclick="WASP_POST.postUpdateView(\''+rowData.postId+'\')">修改</a>';
						var  be = '<a  id="delete-' + role_ID + '" href="javascript:void(0);" class="btn btn-link btn-jqgrid" title="删除" onclick="WASP_POST.deletePost(\''+rowData.postId+'\');" >删除</a>';
						$postList.jqGrid('setRowData',ids[i],{option:ve+se+be});
					}	
				}
			});
		
		$postList.navGrid('#postPage',{edit:false,add:false,del:false,search:false,refreshstate:'current'});
		$postList.jqGrid('setFrozenColumns');

		//$postList.closest(".ui-jqgrid-bdiv").css({'overflow-y': 'scroll'});
		jqGridResize($postList);
		
	});

/**
 * 通过条件进行搜索
 */
function queryByCondtion(flag) {
    var name = $.trim($("#q-name").val());
    var postData = $postList.jqGrid("getGridParam", "postData");
	$.extend(postData, {
		'sp[postTitle]':name
	});
    if (flag) {
    	$postList.trigger("reloadGrid", [{ page: 1 }]); //重新载入Grid表格
    } else {
    	$postList.trigger("reloadGrid"); //重新载入Grid表格
    }
};
